package com.wwl.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class ListDemo {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("Tom");//索引是0
        list.add("Tom2");//1
        list.add("Tom3");
        list.add(1,"Tom1");

        List list2 = new ArrayList();
        list2.add("Rose1");
        list2.add("Rose2");

        list.addAll(2,list2);

        Iterator it = list.iterator();
        while(it.hasNext()){
            String obj = (String) it.next();
            System.out.println("name="+obj);
        }

        System.out.println("第1个元素："+list.get(1));
        System.out.println("Rose1在集合中的位置："+list.indexOf("Rose1"));
        list.remove(1);
        System.out.println(list);
        List list3 = list.subList(1,3);
        System.out.println("List3="+list3);
        list3.set(1,"Alice");
        System.out.println("List3="+list3);

        /*ListIterator li = list3.listIterator();
        while(li.hasNext()){
            System.out.println(li.next());
        }
        while(li.hasPrevious()){
            System.out.println(li.previous());
        }*/

        ListIterator li = list3.listIterator(list3.size());
        while(li.hasNext()){
            System.out.println(li.next());
        }
    }
}
